<template>
  <div class="page-wrapper">
    <el-card>
      <el-tabs>
        <!-- 第一标签页 -->
        <el-tab-pane label="角色管理">
          <el-button type="primary" size="small" @click="isShowDialog=true">新增角色</el-button>
          <el-table :data="roleList" border>
            <el-table-column type="index" label="序号" prop="index" />
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="scope">
                <el-button type="text" @click="showPermDialog(scope.row.id)">分配权限</el-button>
                <el-button type="text" @click="showEditDialog(scope.row.id)">编辑</el-button>
                <el-button type="text" @click="delRole(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination
            :page-size="pageConfig.pagesize"
            :total="total"
            @current-change="currentchange"
          />

        </el-tab-pane>
        <!-- 第二标签页 -->
        <el-tab-pane label="公司信息">
          <el-form label-width="80px">
            <el-form-item label="企业名称">
              <el-input v-model="companyInfo.name" disabled />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="companyInfo.companyAddress" disabled />
            </el-form-item>
            <el-form-item label="公司电话">
              <el-input
                v-model="companyInfo.companyPhone"
                disabled
              />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="companyInfo.mailbox" disabled />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="companyInfo.remarks" disabled />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <el-dialog :title="formLabelAlign.id? '编辑':'添加'" :visible="isShowDialog" @close="btnCancel">
      <el-form ref="addForm" label-width="80px" :model="formLabelAlign" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formLabelAlign.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formLabelAlign.description" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="btnOK">确定</el-button>
        <el-button @click="btnCancel">取消</el-button>
      </template>
    </el-dialog>
    <el-dialog title="分配权限" :visible="isShowPermDialog" @close="btenPermCancel">
      <el-checkbox-group v-model="checkList">

        <el-tree :data="permList" default-expand-all>
          <template v-slot="scope">
            <el-checkbox
              :label="scope.data.id"
            >
              {{ scope.data.name }}
            </el-checkbox>
          </template>
        </el-tree>
      </el-checkbox-group>
      <template #footer>
        <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
        <el-button size="small" @click="btenPermCancel">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getcompanyInfo, getroleInfo, addRole, getroledatele, updateRole, deleteRole, assignPerm } from '@/api/setting'
import { getPermList } from '@/api/permission'
import { listToTree } from '@/utils/index'
export default {
  data() {
    return {
      isShowPermDialog: false,
      companyInfo: {},
      roleList: [],

      permList: [],
      checkList: [],
      commpremId: '',
      pageConfig: {
        page: 2,
        pagesize: 3
      },
      total: 0,
      isShowDialog: false,

      formLabelAlign: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        description: [{ required: true, message: '请输入描述', trigger: 'blur' }]
      }
    }
  },
  async created() {
    // 权限列表选项
    const resPerm = await getPermList()
    // console.log('数据类表', resPerm)
    this.permList = listToTree(resPerm, '0')

    const companyId = this.$store.state.user.userInfo.companyId
    const res = await getcompanyInfo(companyId)
    // console.log(res)
    this.companyInfo = res
    this.loadLIST()
  },
  methods: {

    // 角色分配权限
    async btnPermOK() {
      await assignPerm({
        permIds: this.checkList,
        id: this.commpremId
      })
      this.$message.success('操作成功')
      this.isShowPermDialog = false
    },
    btenPermCancel() {
      this.isShowPermDialog = false
    },
    // 角色权限
    async showPermDialog(resId) {
      this.isShowPermDialog = true
      const res = await getroledatele(resId)
      // console.log('权限', res)
      this.commpremId = resId
      this.checkList = res.permIds
    },
    async currentchange(newPage) {
      console.log('点击了翻页', newPage)
      this.pageConfig.page = newPage
      this.loadLIST()
    },
    async loadLIST() {
      const res2 = await getroleInfo(this.pageConfig)
      console.log(res2)
      this.total = res2.total

      // 分页
      this.roleList = res2.rows.map((inem, index) => {
        return {
          ...inem,
          index: (this.pageConfig.page - 1) * this.pageConfig.pagesize + index + 1
        }
      })
    },
    // 确认按钮
    async btnOK() {
      try {
        // 验证表单
        await this.$refs.addForm.validate()
        // 发请求
        if (this.formLabelAlign.id) {
          await updateRole(this.formLabelAlign)
        } else {
          await addRole(this.formLabelAlign)
        }

        // 提醒用户
        this.$message.success('成功添加')
        // 关闭弹窗
        this.isShowDialog = false
        // 更新表格
        this.loadLIST()
      } catch (error) {
        console.log(error)
      }
    },

    // 取消按钮
    btnCancel() {
      // resetFields
      this.$refs.addForm.resetFields()
      this.isShowDialog = false
    },

    // 编辑按钮
    async showEditDialog(id) {
      const res = await getroledatele(id)
      // console.log('编辑', res)
      this.isShowDialog = true
      this.formLabelAlign = res
    },
    // 删除功能
    async delRole(id) {
      try {
        await this.$confirm('删除')
        // console.log('确认删除')
        await deleteRole(id)
        this.$message.success('删除成功')
        this.loadLIST()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tool-bar {
  margin-bottom: 20px;
}
.page-bar {
  margin-top: 20px;
}
</style>
